package com.practice.mybpapi.domain.dto;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.practice.mybpapi.domain.po.User;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

//泛型分页返回结果
@Data
public class PageBase<T> {
    private Long total;
    private List<T> records;
    private Integer pages;
    
    public PageBase() {
    }
    
    public PageBase(Long total, List<T> records, Integer pages) {
        this.total = total;
        this.pages = pages;
        this.records = records;
    }
    public static PageBase<QueryUserVo> of(Page<User> pageRel){
        List<QueryUserVo> queryUserVos =new ArrayList<>();
        if (pageRel.getRecords()!=null && !pageRel.getRecords().isEmpty()) {
            queryUserVos = BeanUtil.copyToList(pageRel.getRecords(), QueryUserVo.class);
        }
        long pages = pageRel.getPages();
        int i = Integer.parseInt(pages + "");
        //将pages转为int类型
        return new PageBase<>(pageRel.getTotal(), queryUserVos, i);
    }
}